


<!DOCTYPE html>
<html id="htmlId">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
  <title>Coverage Report > HttpJwtTokenUtil</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/idea.min.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.min.js"></script>
  <script type="text/javascript" src="../../js/highlightjs-line-numbers.min.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">com.ie.common.security.utils</a>
</div>

<h1>Coverage Summary for Class: HttpJwtTokenUtil (com.ie.common.security.utils)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Branch, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">HttpJwtTokenUtil</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    40%
  </span>
  <span class="absValue">
    (2/5)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/12)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    7.7%
  </span>
  <span class="absValue">
    (2/26)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<pre>
<code class="sourceCode" id="sourceCode">&nbsp;package com.ie.common.security.utils;
&nbsp;
&nbsp;import cn.hutool.core.util.StrUtil;
&nbsp;import jakarta.servlet.http.HttpServletRequest;
&nbsp;import lombok.AllArgsConstructor;
&nbsp;import lombok.extern.slf4j.Slf4j;
&nbsp;import org.springframework.stereotype.Component;
&nbsp;
&nbsp;import java.io.IOException;
&nbsp;import java.io.UnsupportedEncodingException;
&nbsp;import java.util.HashMap;
&nbsp;import java.util.Map;
&nbsp;
&nbsp;/**
&nbsp; *@Description: HttpJwtTokenUtil
&nbsp; *这是一个Spring注解，用于标识这个类是一个Spring组件，可以被Spring容器自动扫描和管理。Spring会自动将这个类注册为一个Bean，
&nbsp; *可以在其他地方通过依赖注入（Dependency Injection）来使用它
&nbsp; *Slf4j:Lombok注解，用于自动生成一个名为log的日志记录器（Logger），方便在类中进行日志记录
&nbsp; *@Author: xujia
&nbsp; * @Date: 2024/8/29  22:04
&nbsp; * @Param:
&nbsp; * @version: 1.0
&nbsp; * @Return:
&nbsp; **/
&nbsp;@Component
<b class="fc">&nbsp;@Slf4j</b>
<b class="fc">&nbsp;@AllArgsConstructor</b>
&nbsp;public class HttpJwtTokenUtil {
&nbsp;    public static final String KEY_USERNAME = &quot;userName&quot;;
&nbsp;    //	@Autowired
&nbsp;    private final ConfigrarionBean jwtConfig;
&nbsp;    //	@Autowired
&nbsp;    private final JwtTokenUtil jwtUtil;
&nbsp;    /**
&nbsp;     *@Description: 验证HTTP请求中携带的令牌是否有效
&nbsp;     * 具体逻辑如下：
&nbsp;     * 创建一个空的userDetail映射。
&nbsp;     * 从请求头中获取名为jwtConfig.getTokenHeader()的令牌。
&nbsp;     * 如果请求头中的令牌为空或无效，则返回false。
&nbsp;     * 使用jwtUtil.verify()方法验证令牌的合法性，如果验证失败，则返回false。
&nbsp;     * 最后，使用jwtUtil.isExpiration()方法检查令牌是否已过期，并返回检查结果。
&nbsp;     *@Author: xujia
&nbsp;     * @Date: 2024/8/29  22:30
&nbsp;     * @Param: [request]
&nbsp;     * @version: 1.0
&nbsp;     * @Return: boolean
&nbsp;     **/
&nbsp;    public boolean validate(HttpServletRequest request) {
<b class="nc">&nbsp;        Map&lt;String, String&gt; userDetail = new HashMap&lt;&gt;(2);</b>
<b class="nc">&nbsp;        final String requestTokenHeader = request.getHeader(jwtConfig.getTokenHeader());</b>
<b class="nc">&nbsp;        if(StrUtil.isEmpty(requestTokenHeader)) {</b>
<b class="nc">&nbsp;            return false;</b>
&nbsp;        }
<b class="nc">&nbsp;        if(!jwtUtil.verify(requestTokenHeader)) {</b>
<b class="nc">&nbsp;            return false;</b>
&nbsp;        }
<b class="nc">&nbsp;        return jwtUtil.isExpiration(requestTokenHeader);</b>
&nbsp;    }
&nbsp;    /**
&nbsp;     * @Description:获取HTTP请求的POST数据体内容
&nbsp;     * 从请求中获取字符编码，若无则默认使用&quot;UTF-8&quot;。
&nbsp;     * 将字节数组转换为字符串并返回。
&nbsp;     * 若字符编码不支持，则记录错误日志并返回null。
&nbsp;     * @Author: xujia
&nbsp;     * @Date: 2024/8/29  22:35
&nbsp;     * @Param: [request]
&nbsp;     * @version: 1.0
&nbsp;     * @Return: java.lang.String
&nbsp;     **/
&nbsp;    public String getRequestPostStr(HttpServletRequest request) {
<b class="nc">&nbsp;        byte buffer[] = getRequestPostBytes(request);</b>
<b class="nc">&nbsp;        String charEncoding = request.getCharacterEncoding();</b>
<b class="nc">&nbsp;        if (charEncoding == null) {</b>
<b class="nc">&nbsp;            charEncoding = &quot;UTF-8&quot;;</b>
&nbsp;        }
&nbsp;        try {
<b class="nc">&nbsp;            return new String(buffer, charEncoding);</b>
&nbsp;        } catch (UnsupportedEncodingException e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return null;</b>
&nbsp;        }
&nbsp;    }
&nbsp;    /**
&nbsp;     * @Description:获取HTTP请求的内容长度
&nbsp;     * 若长度小于0，返回null。
&nbsp;     * 创建与内容长度相同大小的字节数组。
&nbsp;     * 通过输入流读取请求体内容，并填充到字节数组中。
&nbsp;     * 捕获并记录读取过程中的IO异常。最终返回字节数组。
&nbsp;     * @Author: xujia
&nbsp;     * @Date: 2024/8/29  22:37
&nbsp;     * @Param: [request]
&nbsp;     * @version: 1.0
&nbsp;     * @Return: byte[]
&nbsp;     **/
&nbsp;    public byte[] getRequestPostBytes(HttpServletRequest request) {
<b class="nc">&nbsp;        int contentLength = request.getContentLength();</b>
<b class="nc">&nbsp;        if (contentLength &lt; 0) {</b>
<b class="nc">&nbsp;            return null;</b>
&nbsp;        }
<b class="nc">&nbsp;        byte buffer[] = new byte[contentLength];</b>
&nbsp;        try {
<b class="nc">&nbsp;            for (int i = 0; i &lt; contentLength;) {</b>
&nbsp;                int readlen;
<b class="nc">&nbsp;                readlen = request.getInputStream().read(buffer, i, contentLength - i);</b>
<b class="nc">&nbsp;                if (readlen == -1) {</b>
&nbsp;                    break;
&nbsp;                }
<b class="nc">&nbsp;                i += readlen;</b>
&nbsp;            }
&nbsp;        } catch (IOException e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
&nbsp;        }
<b class="nc">&nbsp;        return buffer;</b>
&nbsp;    }
&nbsp;}
</code>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
      hljs.highlightAll()
      hljs.initLineNumbersOnLoad();
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2025-06-15 18:23</div>
</div>
</body>
</html>
